package com.rjt.data.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import com.rjt.data.service.RedisService;

/**
 * @author 7
 * @create 2019/2/25
 * @since 1.0.0
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public void testHash() {
        JedisConnectionFactory factory =
                (JedisConnectionFactory) redisTemplate.getConnectionFactory();
        factory.setDatabase(15);//dbindex就是你想切换的db
        redisTemplate.setConnectionFactory(factory);
        HashOperations<String, Object, Object> operations = redisTemplate.opsForHash();
        Map map=new HashMap<String,Object>();
        map.put("decs","这是个描述");
        map.put("like","5");
        operations.put("testkey","name","123");
        operations.put("testkey","decs","2323");
        operations.putAll("testkey",map);
        System.out.println("put 完毕");
        redisTemplate.expire("testkey",5, TimeUnit.HOURS);
        System.out.println("时间设置完毕");
        System.out.println(operations.get("testkey","name"));
        System.out.println("name读取完毕");
        ScanOptions scanOptions=null;
        System.out.println( redisTemplate.getExpire("testkey"));
        System.out.println(operations.values("testkey").toString());
        System.out.println(operations.keys("testkey").toString());
    }

    @Override
    public void testList() {
        JedisConnectionFactory factory =
                (JedisConnectionFactory) redisTemplate.getConnectionFactory();
        factory.setDatabase(15);//dbindex就是你想切换的db
        redisTemplate.setConnectionFactory(factory);
        ListOperations<String, String> operations = redisTemplate.opsForList();
        redisTemplate.expire("tanran",5, TimeUnit.MINUTES);
        operations.leftPush("tanran","v587");
        operations.leftPush("tanran","222");

        operations.rightPushAll("tanran","1");
       operations.rightPop("tanran");
        operations.remove("tanran",1,"22222");
       System.out.println(operations.range("tanran",1,100));
    }

    @Override
    public void testSet() {
        JedisConnectionFactory factory =
                (JedisConnectionFactory) redisTemplate.getConnectionFactory();
        factory.setDatabase(15);//dbindex就是你想切换的db
        redisTemplate.setConnectionFactory(factory);
        SetOperations<String, String> setOperations = redisTemplate.opsForSet();
        setOperations.add("testSet","566");
        setOperations.add("testSet","tanrna");
        redisTemplate.expire("testSet",5, TimeUnit.MINUTES);
        Set<String> testSet = setOperations.members("testSet");
        System.out.println(Arrays.toString(testSet.toArray()));
    }

    @Override
    public void testZset() {
        JedisConnectionFactory factory =
                (JedisConnectionFactory) redisTemplate.getConnectionFactory();
        factory.setDatabase(15);//dbindex就是你想切换的db
         ZSetOperations<String, String> test = redisTemplate.opsForZSet();
        test.add("tanranZset","ww",55.0);
        test.add("tanranZset","qq",54.0);
        test.add("tanranZset","dwd",23.0);
        test.add("tanranZset","通天塔",23.0);
        test.add("tanranZset","通塔",66.0);
        redisTemplate.expire("tanranZset",5, TimeUnit.HOURS);
        Set<String> tanranZset = test.range("tanranZset", 0, test.size("tanranZset"));
        System.out.println(Arrays.toString(tanranZset.toArray()));
    }


}